<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id='app'>
        <cpn :number1="num1" :number2="num2" @number1change="number1change" @number2change="number2change"></cpn>
    </div>
    <template id="cpn">
        <!-- 每个组件都有一个根 -->
        <div>
            <h2>props:{{number1}}</h2>
            <h2>data:{{dnumber1}}</h2>
            <input type="text" :value="dnumber1" @input="num1Input">
            <h2>props:{{number2}}</h2>
            <h2>data:{{dnumber2}}</h2>
            <input type="text" :value="dnumber2" @input="num2Input">
        </div>
    </template>
    <script src='../js/vue.js'></script>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                num1: 1,
                num2: 0
            },
            methods: {
                number1change(value) {
                    this.num1 = parseFloat(value)
                },
                number2change(value) {
                    this.num2 = parseFloat(value)
                }
            },
            components: {
                cpn: {
                    template: '#cpn',
                    props: {
                        number1: Number,
                        number2: Number
                    },
                    data() {
                        return {
                            dnumber1: this.number1,
                            dnumber2: this.number2
                        }
                    },
                    methods: {
                        num1Input(event) {
                            //1.将input中的value赋值到dnumber1中
                            this.dnumber1 = event.target.value;

                            // 2.为了让父组件可以修改值，发出一个事件
                            this.$emit('number1change', this.dnumber1);

                            //3.同时修改dnumber2的值
                            this.dnumber2 = this.dnumber1 * 100;
                            this.$emit('number2change', this.dnumber2)
                        },
                        num2Input(event) {
                            //1.将input中的value赋值到dnumber2中
                            this.dnumber2 = event.target.value;

                            // 2.为了让父组件可以修改值，发出一个事件
                            this.$emit('number2change', this.dnumber2);

                            //3.同时修改dnumber1的值
                            this.dnumber1 = this.dnumber1 / 100;
                            this.$emit('number1change', this.dnumber1)
                        }
                    }

                }
            },
            computed: {

            },
            filters: {},
        })
    </script>
</body>

</html>